﻿using PortableRiotGames.Interfaces.Game;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;

namespace PortableRiotGames.ConsoleTests
{
    internal class Program
    {
        // Change this key to your own Developer key.
        private static string _key = ConfigurationManager.AppSettings["RiotGames.DeveloperKey"].ToString();

        // helloworld65.
        private static long _summonerID = 20934774; 

        // Like a Machine
        private static long _summonerID2 = 21267565;

        private static void Main(string[] args)
        {
            RiotGamesWebAPI.SetDeveloperKey(_key);
            RiotGamesWebAPI.SetDefaultRegion(Region.NA);

            //Champion();
            Recent(); // I have terrible sight ward purchase averages =.="
            //var response = RiotGamesAPI.League().GetLeagues(_summonerID2).GetResponse();
            //var response = RiotGamesAPI.Stats().GetRanked(_summonerID).GetResponse();
            //var response = RiotGamesAPI.Stats().GetSummary(_summonerID2).GetResponse();
            //var response = RiotGamesAPI.Summoner().GetRunes(_summonerID2).GetResponse();
            //var response = RiotGamesAPI.Summoner().GetSummoner(_summonerID2).GetResponse();
            //var response = RiotGamesAPI.Summoner().GetSummoner("helloworld65").GetResponse();
            //var response = RiotGamesAPI.Summoner().GetSummoners(_summonerID, _summonerID2).GetResponse();
            //var response = RiotGamesAPI.Team().GetTeams(_summonerID2).GetResponse();

            Console.ReadKey();
        }

        #region Champion

        private static void Champion()
        {
            Console.WriteLine("champion-v1.1 [EUNE, EUW, NA]");
            Console.WriteLine("-----------------------------");
            var response = RiotGamesWebAPI.Champion().GetChampions().FreeToPlay().Regions(Region.NA).GetResponse();

            foreach (var r in response.Responses)
            {
                Console.WriteLine("Region: " + r.Region.ToString());
                Console.WriteLine("-----------------------------");

                if (r.HttpStatus == Infrastructure.HttpStatus.OK)
                {

                    foreach (var champion in r.Champions)
                    {
                        Console.WriteLine(string.Format("{0}: {1}", champion.ID, champion.Name));
                        Console.WriteLine(string.Format("Attack Rank:     [{0}]", GetChampionRank(champion.AttackRank)));
                        Console.WriteLine(string.Format("Defence Rank:    [{0}]", GetChampionRank(champion.DefenseRank)));
                        Console.WriteLine(string.Format("Magic Rank:      [{0}]", GetChampionRank(champion.MagicRank)));
                        Console.WriteLine(string.Format("Difficulty Rank: [{0}]", GetChampionRank(champion.DifficultyRank)));
                        Console.WriteLine("-----------------------------");
                    }
                }
                else
                {
                    Console.WriteLine(r.ErrorMessage + "(" + (int)r.HttpStatus + ")");
                }
            }
        }

        private static string GetChampionRank(int rank)
        {
            string value = string.Empty;
            for (int i = 0; i < 10; i++)
            {
                if (i <= rank)
                {
                    value += "X";
                }
                else
                {
                    value += "-";
                }
            }
            return value;
        }

        #endregion Champion

        #region Game

        public static void Recent()
        {
            Console.WriteLine("game-v1.2 [NA, EUNE, EUW]/Recent");
            Console.WriteLine("-----------------------------");
            Console.WriteLine("Summoner ID: {0}", _summonerID);
            Console.WriteLine("Summoner Name: {0}", RiotGamesWebAPI.Summoner().GetSummoner(_summonerID).GetResponse().Responses.FirstOrDefault().Name);
            var champions = RiotGamesWebAPI.Champion().GetChampions().GetResponse().Responses.FirstOrDefault().Champions;
            var responses = RiotGamesWebAPI.Game().GetRecent(_summonerID).GetResponse();
            var response = responses.Responses.FirstOrDefault(x => x.Region == Region.NA);

            if (response.HttpStatus == Infrastructure.HttpStatus.OK)
            {
                double wins = response.Games.SelectMany(x => x.Statistics).Where(x => x.Name == "WIN" && x.Value == 1).Count();
                Console.WriteLine("Win percentage over last 10 games: {0}%", (wins / response.Games.Count) * 100);
                RecentAdvancedStats(response.Games.SelectMany(x => x.Statistics));
                Console.WriteLine("-----------------------------");
                foreach (var game in response.Games.Take(3))
                {
                    Console.WriteLine("Date: {0}", game.CreateDate);
                    Console.WriteLine("Champion: {0}", champions.FirstOrDefault(x => x.ID == game.ChampionID).Name);
                    Console.WriteLine("Map: {0}", game.Map.ToString());
                    Console.WriteLine("GameType: {0}", game.GameType.ToString());
                    Console.WriteLine("GameMode: {0}", game.GameMode.ToString());
                    Console.WriteLine("Stats");
                    Console.WriteLine("-----------------------------");

                    foreach (var stat in game.Statistics)
                    {
                        Console.WriteLine("{0}: {1}", stat.Name, stat.Value);
                    }
                }
            }
        }

        private static void RecentAdvancedStats(IEnumerable<GetRecentResponseStatistic> stats)
        {
            Console.WriteLine("Average Game Time: {0}", TimeSpan.FromSeconds(stats.Where(x => x.Name == "TIME_PLAYED").Average(x => x.Value)).TotalMinutes);
            Console.WriteLine("Average Gold Earned: {0}", stats.Where(x => x.Name == "GOLD_EARNED").Average(x => x.Value));
            Console.WriteLine("Average Gold Spent: {0}", stats.Where(x => x.Name == "GOLD_SPENT").Average(x => x.Value));
            Console.WriteLine("Average Champion Killed: {0}", stats.Where(x => x.Name == "CHAMPIONS_KILLED").Average(x => x.Value));
            Console.WriteLine("Average Deaths: {0}", stats.Where(x => x.Name == "NUM_DEATHS").Average(x => x.Value));
            Console.WriteLine("Average Assists: {0}", stats.Where(x => x.Name == "ASSISTS").Average(x => x.Value));
            var sightWardStats = stats.Where(x => x.Name == "SIGHT_WARDS_BOUGHT");
            Console.WriteLine("Average Sight Wards Purchased: {0}", sightWardStats.Average(x => x.Value));

            
        }

        #endregion Game

        #region League
        
        #endregion
    }
}